-- XML Parser based on the work of Roberto Ierusalimschy

XMLParser = class("XMLParser")

---

function XMLParser:initialize(fileName)

	local file = io.open(gRootPath .. "assets\\" .. fileName, "r")
	local inputString = file:read("*all")
	
	self.data = self:createData(inputString)
end

---

function XMLParser:parseargs(s)

  local arg = {}
  string.gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
    arg[w] = a
  end)
  return arg
end

---

function XMLParser:createData(inputString)

  local stack = {}
  local top = {}
  table.insert(stack, top)
  local ni,c,label,xarg, empty
  local i, j = 1, 1
  
  while true do
    ni,j,c,label,xarg, empty = string.find(inputString, "<(%/?)([%w:]+)(.-)(%/?)>", i)
    if not ni then break end
    local text = string.sub(inputString, i, ni-1)
    if not string.find(text, "^%inputString*$") then
      table.insert(top, text)
    end
    if empty == "/" then  -- empty element tag
      table.insert(top, {label=label, xarg=XMLParser:parseargs(xarg), empty=1})
    elseif c == "" then   -- start tag
      top = {label=label, xarg=XMLParser:parseargs(xarg)}
      table.insert(stack, top)   -- new level
    else  -- end tag
      local toclose = table.remove(stack)  -- remove top
      top = stack[#stack]
      if #stack < 1 then
        error("nothing to close with "..label)
      end
      if toclose.label ~= label then
        error("trying to close "..toclose.label.." with "..label)
      end
      table.insert(top, toclose)
    end
    i = j+1
  end
  
  local text = string.sub(inputString, i)
  if not string.find(text, "^%inputString*$") then
    table.insert(stack[#stack], text)
  end
  
  if #stack > 1 then
    error("unclosed "..stack[#stack].label)
  end
  
  return stack[1]
end

---

function XMLParser:getProperty(table, propertyName)

	assert(table ~= nil, "If the root table must be considered, pass false for the table parameter")
	
	if table == false then
	
		table = self.data
	end
	
	if table.label == propertyName then
	
		return table[1]
		
	elseif table.xarg and table.xarg[propertyName] then
	
		return table.xarg[propertyName]
		
	elseif table[propertyName] then
	
		return table[propertyName]
	else
		for k, v in pairs(table) do
		
			if type(v) == "table" then
			
				local value  = self:getProperty(v, propertyName, true)
				
				if value then
				
					return value
				end
			end
		end
	end	

	return nil
end

---

function XMLParser:getSubTables(table, subTableName, recursiveCall)

	assert(table ~= nil, "If the root table must be considered, pass false for the table parameter")
	
	if table == false then
	
		table = self.data
	end
	
	if not recursiveCall then
	
		self.collectedTables = {}
	end
	
	if table.label == subTableName then
	
		self.collectedTables[#self.collectedTables + 1] = table
	else
		for k, v in pairs(table) do
		
			if type(v) == "table" then
			
				self:getSubTables(v, subTableName, true)
			end
		end
	end
	
	if not recursiveCall then
	
		return self.collectedTables
	end
end

---

function XMLParser:printData()

	debug.printTable(self.data, 50)
end